package com.isay.app.honey.ui.home;

import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.isay.app.honey.R;
import com.isay.app.honey.manager.NetworkManager;
import com.isay.app.honey.ui.cash.history.HistoryAdapter;
import com.isay.frameworklib.okhttp.ICommonCallback;
import com.isay.frameworklib.ui.BaseFragment;
import com.isay.frameworklib.user.UserManager;
import com.isay.frameworklib.utils.ClickUtils;
import com.isay.frameworklib.utils.ToastUtils;
import com.isay.frameworklib.utils.WebUrlUtils;
import com.isay.frameworklib.utils.glide.GlideUtils;
import com.isay.frameworklib.widget.GlideImageLoader;
import com.isay.frameworklib.widget.GridItemDecoration;
import com.isay.frameworklib.widget.dialog.MessageBox;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by cywu4 on 2018/8/9.
 */

public class HomeFragment extends BaseFragment implements View.OnClickListener {

    private TextView mTvNotice;
    private Banner mViewBanner;
    private RecyclerView mRecyclerView;
    protected HomeRecyclerAdapter mRecyclerAdapter;


    private List<BannerItemInfo> mBannerList = new ArrayList<>();

    public static HomeFragment getInstance() {
        return new HomeFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }


    @Override
    protected void initView(View rootView) {
        mTvNotice = rootView.findViewById(R.id.tv_notice);
        mTvNotice.setSelected(true);
        //RecyclerView
        mRecyclerView = rootView.findViewById(R.id.recycler_view);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),3);
        GridItemDecoration divider = new GridItemDecoration.Builder(getContext())
                .setHorizontalSpan(R.dimen.px1)
                .setVerticalSpan(R.dimen.px1)
                .setColorResource(R.color.grey_f2f2f2)
                .setShowLastLine(true)
                .build();
        mRecyclerView.addItemDecoration(divider);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mRecyclerAdapter = new HomeRecyclerAdapter(getActivity());
        mRecyclerView.setAdapter(mRecyclerAdapter);
        //banner
        mViewBanner = rootView.findViewById(R.id.view_banner);
        mViewBanner.setImageLoader(new GlideImageLoader());
        mViewBanner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                if (position < mBannerList.size()) {
                    WebUrlUtils.openUrl(getActivity(), mBannerList.get(position).getPath());
                }
            }
        });
    }

    @Override
    protected void initData() {
        query();
/*        List<HomeItemInfo> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            HomeItemInfo info = new HomeItemInfo();
            info.setName("name " + i);
        }
        mRecyclerAdapter.addData(true, list);*/
    }


    @Override
    public void onClick(View v) {
        if (ClickUtils.isFastClick()) {
            return;
        }
        switch (v.getId()) {

            default:
                break;
        }
    }

    private void query() {
        NetworkManager.home(new ICommonCallback() {
            @Override
            public void onSuccess(int ret, String msg, String content) {
                if (ret == 200) {
                    List<HomeItemInfo> list = new ArrayList<>();
                    String notice = "";
                    try {
                        JSONObject jsonObject = new JSONObject(content);
                        //解析通知
                        notice = jsonObject.optString("notice");
                        //解析热门下载
                        JSONArray array = jsonObject.optJSONArray("applyList");
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject object = array.optJSONObject(i);
                            HomeItemInfo info = new HomeItemInfo(object.optString("id"),
                                    object.optString("name"),
                                    object.optString("logo"),
                                    object.optString("an_path"));
                            list.add(info);
                        }
                        //解析banner
                        JSONArray bannerArray = jsonObject.optJSONArray("bannerList");
                        for (int i = 0; i < bannerArray.length(); i++) {
                            JSONObject object = bannerArray.optJSONObject(i);
                            BannerItemInfo info = new BannerItemInfo(object.optString("image"),
                                    object.optString("path"));
                            mBannerList.add(info);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    //显示通知
                    mTvNotice.setText(notice);
                    //显示热门下载
                    mRecyclerAdapter.addData(true, list);
                    mRecyclerAdapter.notifyDataSetChanged();
                    //显示banner
                    List<String> bannerUrl = new ArrayList<>();
                    for (BannerItemInfo info : mBannerList) {
                        bannerUrl.add(info.getImage());
                    }
                    mViewBanner.setImages(bannerUrl);
                    mViewBanner.start();
                } else {
                    ToastUtils.show(msg);
                }

            }

            @Override
            public void onFailure(Exception e) {
                int i = 0;
            }
        });
    }


}
